import Router from 'express';



const flightRouter = Router();

//增加航班信息
flightRouter.post('/addFlight', async (req, res) => {
    const {
        originPlace,
        destination,
        originAirport,
        desAirport,
        originDate,
        originTime,
        desTime,
        company,
        planeNum,
        planeType,
        price
    } = req.body
    if (!originPlace) return res.fail('始发地不能为空')
    if (!destination) return res.fail('目的地不能为空')
    if (!originAirport) return res.fail('始发机场不能为空')
    if (!desAirport) return res.fail('目的地机场不能为空')
    if (!originDate) return res.fail('始发日期不能为空')
    if (!originTime) return res.fail('到达时间不能为空')
    if (!desTime) return res.fail('到达时间不能为空')
    if (!company) return res.fail('飞机归属公司不能为空')
    if (!planeNum) return res.fail('飞机编号不能为空')
    if (!planeType) return res.fail('飞机类型不能为空')
    if (!price) return res.fail('机票价格不能为空')
    try {
        const db = req.db
        const addFlight = {
            originPlace,
            destination,
            originAirport,
            desAirport,
            originDate,
            originTime,
            desTime,
            company,
            planeNum,
            planeType,
            price
        }
        if (originPlace) addFlight.originPlace = originPlace
        await db.Flight.create({
            data: addFlight
        })
        res.ok({ success: true, info: '添加成功' })
    } catch (e) {
        res.fail('添加失败', e)
    }


})

//删除航班信息
flightRouter.post('/delFlight', async (req, res) => {
    const {
        id,
        originDate,
        planeNum
    } = req.body
    console.log(req.body);
    if (!originDate) return res.fail('id不能为空')
    if (!originDate) return res.fail('日期不能为空')
    if (!planeNum) return res.fail('航班号不能为空')
    try {
        const db = req.db
        const delFlight = await db.Flight.delete({
            where: {
                id: Number(id),
                originDate: String(originDate),
                planeNum: String(planeNum)
            }
        })
        res.ok({ success: true, info: '删除成功' })
    } catch (e) {
        console.log(e)
        res.fail('删除失败', e)
    }



})

//修改航班信息
flightRouter.post('/updateFlight', async (req, res) => {
    const {
        id,
        originPlace,
        destination,
        originAirport,
        desAirport,
        originDate,
        originTime,
        desTime,
        company,
        planeNum,
        planeType,
        price
    } = req.body
    if (!id) return res.fail('id不能为空')
    try {
        const db = req.db
        const updateFlight = {}
        if (originPlace) updateFlight.originPlace = originPlace
        if (destination) updateFlight.destination = destination
        if (originAirport) updateFlight.originAirport = originAirport
        if (desAirport) updateFlight.desAirport = desAirport
        if (originDate) updateFlight.originDate = originDate
        if (originTime) updateFlight.originTime = originTime
        if (desTime) updateFlight.desTime = desTime
        if (company) updateFlight.company = company
        if (planeNum) updateFlight.planeNum = planeNum
        if (planeType) updateFlight.planeType = planeType
        if (price) updateFlight.price = price
        await db.Flight.update({
            data: updateFlight,
            where: {

                id: Number(id)
            }
        })
        res.ok({ success: true, info: '修改成功' })
    } catch (e) {
        console.log(e);
        res.fail('修改失败')
    }

})

//查找航班信息
flightRouter.post('/findFlight', async (req, res) => {
    const {
        id,
        originPlace,
        destination,
        originAirport,
        desAirport,
        originDate,
        originTime,
        desTime,
        company,
        planeNum,
        planeType,
        price
    } = req.body
    try {
        const db = req.db
        const findFlight = await db.Flight.findMany({
            where: {
                id,
                originPlace,
                destination,
                originAirport,
                desAirport,
                originDate,
                originTime,
                desTime,
                company,
                planeNum,
                planeType,
                price
            }
        })
        res.ok({ success: true, info: '查询成功', data: findFlight })
    } catch (e) {
        console.log(e);
        res.fail('查询失败')
    }


})



export default flightRouter;